package com.njzxw.fs.serversys.repository;

import com.njzxw.fs.commonutil.db.ZxwLangDriver;
import com.njzxw.fs.serversys.bean.CfgBean;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

@Mapper
public interface CfgReq {

    @Select("select a.`id`, a.`groupname`,a.`groupcode`, a.`siglesubitem`,a.is_add_item from t_deploy_group a where a.`state` = 1")
    List<Map<String,Object>> getGroup();

    @Select("select a.big_type,a.preinstall_val,a.input_type,a.`id`, a.`groupname`,a.state, a.`groupcode`,a.oper_id,a.oper_date,a.init,a.remark,a.weixinxcx,a.is_reload,a.is_app, a.`siglesubitem`,a.is_add_item " +
            " from t_deploy_group a " +
            "where a.`state` = 1 and groupcode = #{group_id}")
    Map<String,Object> getGroupMap(CfgBean cfgBean);

    @Select("select count(1) from t_deploy_group a " +
            "where  groupcode = #{group_id} and a.id != #{id}")
    @Lang(ZxwLangDriver.class)
    Integer getGroupCodeCnt(CfgBean cfgBean);

    @Select("select count(1) from t_deploy_item a " +
            "where  group_id = #{groupcode}")
    Integer getItemCnt(CfgBean cfgBean);

    @Select("select a.big_type,a.preinstall_val,a.input_type,a.`id`,a.item_nums item_num, a.`groupname`,a.state, a.`groupcode`,a.oper_id,a.oper_date,a.init,a.remark,a.weixinxcx,a.is_reload,a.is_app, a.`siglesubitem`,a.is_add_item from t_deploy_group a " +
            "where groupcode = #{groupcode}")
    CfgBean getBigMap(CfgBean cfgBean);


    @Select("select a.id, a.name, a.`orderby`, a.values,a.remark from t_deploy_item a where a.`group_id` = #{group_id} and a.`state` = 1")
    List<Map<String,Object>> getGroupItemList(CfgBean cfgBean);

    @Delete("delete from t_deploy_group where groupcode = #{group_id}")
    int deleteBigItem(CfgBean cfgBean);

    @Delete("delete from t_deploy_item where group_id = #{group_id}")
    int deleteItem(CfgBean cfgBean);

    @Delete("delete from t_deploy_item where id in (#{ids})")
    @Lang(ZxwLangDriver.class)
    Integer deleteItems(CfgBean cfgBean);

    @Update("update t_deploy_item a set a.`values` = #{values},a.name=#{name},a.remark=#{remark},a.orderby=#{orderby} where a.id = #{id}")
    int updateItem(CfgBean cfgBean);

    @Insert("insert into t_deploy_item(#{cfgBean})")
    @Lang(ZxwLangDriver.class)
    int insert(CfgBean cfgBean);

    @Insert("insert into t_deploy_group(big_type,preinstall_val,input_type,groupname,groupcode,oper_id,init,remark,siglesubitem,is_add_item,weixinxcx,is_reload,is_app,item_nums)" +
            "values(#{big_type},#{preinstall_val},#{input_type},#{groupname},#{groupcode},#{oper_id},#{init},#{remark},#{siglesubitem},#{is_add_item},#{weixinxcx},#{is_reload},#{is_app},#{item_nums})")
    Integer insertBig(CfgBean cfgBean);

    @Update("update t_deploy_group a set a.big_type = #{big_type},a.preinstall_val = #{preinstall_val},a.input_type = #{input_type},a.groupname = #{groupname},a.groupcode = #{groupcode},a.oper_id = #{oper_id}, " +
            "       a.init = #{init} ,a.item_nums = #{item_nums},a.remark = #{remark},a.siglesubitem = #{siglesubitem},a.is_add_item = #{is_add_item},a.weixinxcx = #{weixinxcx}," +
            "       a.is_reload = #{is_reload},a.is_app = #{is_app} where a.id = #{id}")
    Integer updateBig(CfgBean cfgBean);

    @Select("select b.name,b.values,b.remark,a.groupname,a.groupcode,a.siglesubitem " +
            " from t_deploy_group a,t_deploy_item b\n" +
            "where a.groupcode = b.group_id \n" +
            "  and a.state = 1\n" +
            "\tand b.state = 1\n" +
            "\tand a.weixinxcx = #{weixinxcx} \n" +
            "\tand a.init = #{init} \n" +
            "\torder by b.orderby")
    @Lang(ZxwLangDriver.class)
    List<Map<String,Object>> getConfig(CfgBean cfgBean);

    @Select("select a.groupcode from t_deploy_group a where a.state = 1 and a.weixinxcx = #{weixinxcx} and a.init = #{init} and a.is_app = #{is_app}")
    @Lang(ZxwLangDriver.class)
    List<Map<String,Object>> getGroupList(CfgBean cfgBean);

    @Select("select a.big_type,b.id,a.groupcode,a.is_reload,a.groupname,CONCAT(a.remark,ifnull(b.`explain`,'')) `explain`,b.`values`,a.input_type,a.preinstall_val \n" +
            "  from t_deploy_group a left join t_deploy_item b on a.groupcode = b.group_id where a.siglesubitem = 0 " +
            "and a.state = 1 and a.shop_id = #{shop_id} and a.big_type=#{big_type}")
    @Lang(ZxwLangDriver.class)
    List<Map<String,Object>> getGroupSignList(CfgBean cfgBean);

    @Select("select a.big_type,a.groupcode,a.item_nums item_num,a.preinstall_val,a.input_type,a.shop_id,a.merchant_id,a.is_add_item,b.id,a.is_reload,a.groupname,a.remark group_remark,b.`explain`,b.`values` ,b.orderby,b.remark,b.`name`,a.input_type,a.preinstall_val\n" +
            "  from t_deploy_group a left join t_deploy_item b on a.groupcode = b.group_id where a.siglesubitem = 1 " +
            "and a.state = 1 and a.shop_id = #{shop_id} and a.big_type=#{big_type} order by a.groupcode,b.orderby")
    @Lang(ZxwLangDriver.class)
    List<Map<String,Object>> getGroupMaxList(CfgBean cfgBean);

    @Insert("insert into t_backups(table_name,oper_id,data)values(#{table_name},#{oper_id},#{data})")
    Integer insertBackUps(@Param("table_name") String table_name, @Param("oper_id") String oper_id,
                          @Param("data") String data);

    @Select("select a.id,DATE_FORMAT(a.oper_date,'%Y-%m-%d %T') oper_date,b.`name`\n" +
            "  from t_backups a left join t_manager_user b on b.id = a.oper_id where a.table_name = #{table_name} order by a.oper_date desc ")
    List<Map<String,Object>> getBackUpsList(@Param("table_name") String table_name);

    @Select("select a.`data`\n" +
            "  from t_backups a where a.id = #{id} ")
    String getBackUps(@Param("id") String id);

    @Select("select GROUP_CONCAT(a.id) from (select b.id from t_deploy_item b where b.group_id = #{groupcode} order by b.id desc  LIMIT 0,${item_num}) a")
    String getItemDelete(CfgBean cfgBean);

}
